﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using WebSocket.Common;

namespace WebSocket.Client
{
    class Client
    {
        static void Main(string[] args)
        {
            ListenIPAddress lia = new ListenIPAddress();

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(lia.GetIPEndPoint());

            string sendStr = "hello!This is a socket test";
            byte[] bs = Encoding.ASCII.GetBytes(sendStr);//把字符串编码为字节
            Console.WriteLine("Send Message");
            socket.Send(bs, bs.Length, 0);//发送信息

            string recvStr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = socket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
            recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);

            socket.Close();

            Console.WriteLine("client get message:{0}", recvStr);//显示服务器返回信息
            Console.ReadLine();
        }
    }
}
